

$(window).resize(function () {



    //背景调试
    if($(window).height() / $(window).width()  < 9/16){
        $('.lottery-bg').css('background-size','100% auto');
    }else {
        $('.lottery-bg').css('background-size','auto 100%');
    }


    if($(window).width() < $(window).height()){
        //console.log(($(window).height()/'3')+'px')
        let horizontalPos = ($(window).height()/'3.5')+'px'
        $('.lottery-tabs').css('margin',`${horizontalPos} auto`);
        $('.lottery-tabs .lottery-tabs-content .prize .people-absolute').css('width','100%');
        $('.lottery-tabs').css('padding','0');
        $('.lottery-tabs .lottery-tabs-content .prize .goods-name').css('margin-top','10%');
    }else {
        let verticalPos = ($(window).height()/'9')+'px'
        $('.lottery-tabs').css('margin',`${verticalPos} auto`);
    }



});

$(window).resize();



function getOs()
{
    var OsObject = "";
    if(navigator.userAgent.indexOf("MSIE")>0) {
        return "MSIE";
    }
    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
        $('.lottery-tabs .lottery-tabs-nav .special .add').css('top','7px');
        $('.lottery-tabs .lottery-tabs-nav .special .subtract').css('top','7px');
        return "Firefox";
    }
    if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
        return "Safari";
    }
    if(isCamino=navigator.userAgent.indexOf("Camino")>0){
        return "Camino";
    }
    if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
        return "Gecko";
    }

}
//console.log("您的浏览器类型为:"+getOs());



let lottery = {
        //图片数组
        personArray: [],
        //开始按钮取反
        f: false,
        //图片指针
        imgPosition: 0,
        //定时器
        timer:'',
        t:1,
        //接收中奖者的头像路径和名称
        rewardInfo:[]
    }

    //获取当前点击 li 的长度
    let peopleLi,
    //获取当前点击选项卡的下标 + 1
        tabIndex,
        //获取自定义选项卡点击时候的长度，默认显示一个抽奖人员，所以默认是 1
        specialLength = 1;


    (function () {
        /* 首先获取地址里是否有cus=1这个参数，如果有则显示自定义按钮 */
        function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
            if (r != null) return unescape(r[2]);
            return null;
        }
        let urlOid = GetQueryString('cus');
        //console.log(urlOid)
        if(urlOid==1){
            $('.lottery-tabs .lottery-tabs-nav .special').css('visibility','visible');
        }

        /* 预先加载微信签到人头像和名字 */
        let signUrl = './js/sign_list.json';
        //let signUrl = 'http://192.168.0.9:3000/wechat/api/sign/list';
        //let signUrl = 'https://srv2.zunyaohui.com/wechat/api/sign/list';
        $.ajax({
            url: signUrl,
            type: 'GET',
            data: {},
            dataType: 'json',   //json or jsonp
            success: function (data) {
                console.log(data);
                if(data){
                    /* 获取图片和名字的数据并 push 到新的空数组里 */
                    $.each(data, function (index, item) {
                        //console.log(item['h'])
                        //console.log(item['n'])
                        //console.log(item['n'].slice(0,11))
                        lottery.personArray.push({
                            headSrc: item['h'],
                            name: item['n'] ? item['n'].slice(0,11) : ''
                        });
                    });

                    //console.log( lottery.personArray);
                    //将获取到的图片预先获取出来并加载到空数组里并隐藏起来
                    for (i in lottery.personArray) {
                        //console.log(lottery.personArray[i]);
                        //console.log(lottery.personArray[i].headSrc);
                        $(`<div><img src="${lottery.personArray[i].headSrc}" alt=""></div>`).appendTo('.reload-img');
                        //console.log(data[i].n);
                    }

                }else{
                    //console.log('暂无 signlist 数据')
                }
            }
        });
        /* 预先加载微信签到人头像和名字结束 */
    })()

    /* 选项卡 */
    $('#lottery-tabs-nav').find('.tab').click(function () {
        $(this).addClass('on').siblings().removeClass('on');
        $('.lottery-tabs-content').find('.prize').eq($(this).index()).addClass('show').siblings().removeClass('show');

        // 获取当前点击选项卡的下标 + 1
        tabIndex = $(this).index() + 1;
        //console.log(tabIndex);

    })
    /* 选项卡结束 */





    /* 定时器封装 */
    function timeOut() {
        if (lottery['t'] <= 0) {
            clearTimeout(lottery['timer']);
            //一定要return，不然下面又创建了新的定时器
            return;
        }
        //遍历当前 所有的 li 并获取下标
        for (let i = 0; i < peopleLi.length; i++) {
            //console.log(i);
            // console.log(peopleLi[i])
            // console.log(lottery.imgPosition)
            //console.log(lottery.personArray)
            $(peopleLi[i]).find('img').attr('src', lottery.personArray[lottery.imgPosition].headSrc);
            $(peopleLi[i]).find('span').text(lottery.personArray[lottery.imgPosition].name);
            lottery.imgPosition++;
            lottery.imgPosition = lottery.imgPosition > lottery.personArray.length - 1 ? 0 : lottery.imgPosition;
        }

        lottery['timer'] = setTimeout("timeOut()", 100);
    }
    /* 定时器封装结束 */

//曲工要求自定义按钮先保留一个抽奖的人
$('<li class="people-li add-li"><img src="images/question.png" alt=""><span class="people-name"></span></li>').appendTo($('.special-prize .people-ul'));
$('.special-prize .lottery-btn').css('visibility','visible');
//曲工要求自定义按钮先保留一个抽奖的人结束

/* '自定义'按钮 */
//点击'加号'时增加 li
$('.add').click(function () {
    //初始化，当自定义抽奖抽过一次后，再次点击 + 号时，原来抽过的人和名字还会保留在上面，所以需要初始化
    $('.special-prize .people-ul .people-li img').attr('src','images/question.png');
    $('.special-prize .people-name').text('');


    //如果 ul 里面 没有 li 则点击增加 一个li ,如果 ul 里面有 >1 并且 <5 个li 则点击增加 li
    ($('.add-li').length<5) ? $('<li class="people-li add-li"><img src="images/question.png" alt=""><span class="people-name"></span></li>').appendTo($('.special-prize .people-ul')) : null;

    //获取自定义选项卡点击时候的长度并传给请求数据当做参数
    //console.log($('.add-li').length);
    specialLength = $('.add-li').length;

    //将'开始'按钮显示出来
    $('.special-prize .lottery-btn').css('visibility','visible');


});
//点击'减号'时减少 li
$('.subtract').click(function () {
    $('.add-li').eq(0).remove();
    //console.log($('.add-li').length);
    specialLength = $('.add-li').length;
    //当没有li的长度后，将'开始'按钮隐藏
    if($('.add-li').length==0){
        $('.special-prize .lottery-btn').css('visibility','hidden');
    }
})

/* '自定义'按钮结束 */
    /* 点击开始按钮 */
    //http://wx.zunyaohui.com/activity/lottery/draw?level=3&number=3
    //let clickDraw = 'https://srv2.zunyaohui.com/activity/lottery/draw';
    let clickDraw = './js/draw.json';
    $('.lottery-btn').click(function () {

        //获取当前点击 li 的长度
        //console.log($(this).prev().children().children().length)
        peopleLi = $(this).prev().children().children();
        //console.log(peopleLi)
        if(lottery['f']=!lottery['f']){
            //每次点击之前先清空数组
            lottery.rewardInfo = [];
            $('.lottery-btn p').text('停止');
            //激活定时器
            timeOut();
            //点击开始发送请求
            $.ajax({
                url:clickDraw,
                type:'GET',
                data:{
                    level: tabIndex,
                    number: specialLength
                },
                dataType:'json',       //json or jsonp
                success:function (data) {
                    console.log(data)
                    if(data.err==2){
                        console.log('未指定奖品数量')
                    }else if(data.err==3){
                        console.log('奖品库存不足')
                    }else {
                        //console.log(data);
                        //console.log(data.rows);
                        $.each(data.rows,function (i,val) {
                            //获取中奖者的，图片地址，名字
                            /*console.log(val['h']);
                            console.log(val['n']);*/
                           /* val['h'].push(lottery.rewardImg);
                            val['n'].push(lottery.rewardName);*/
                            lottery.rewardInfo.push({
                                rewardSrc: val['h'],
                                rewardName: val['n'] ? val['n'].slice(0,11) : ''
                            });
                        });
                    };
                }
            });


        }else {

            $('.lottery-btn p').text('开始');
            //清除定时器
            clearTimeout(lottery['timer']);

            if(lottery.rewardInfo.length){ //如果 lottery.rewardInfo 里有数据的话，则。。。
                //点击停止，将接收到的中奖者头像和名字，渲染到页面上
                for (i in lottery.rewardInfo) {     //1. 遍历获取到的中奖者头像和名字
                    //console.log(lottery.rewardInfo[i]);
                    //console.log(lottery.rewardInfo[i].rewardSrc);
                    for(let j = 0; j< peopleLi.length; j++){    //2. 遍历当前奖品等级有几个抽奖人员参加

                        //将 1 和 2  一一对应渲染到页面上，并且判断，如果 lottery.rewardInfo 里剩余的数据小于当前 li 的长度了，则把剩下的用 问号 显示
                        $(peopleLi[j]).find('img').attr('src', lottery.rewardInfo[j] ? lottery.rewardInfo[j].rewardSrc : 'images/question.png')
                        $(peopleLi[j]).find('span').text(lottery.rewardInfo[j]? lottery.rewardInfo[j].rewardName:'');
                    }
                }
            }else { //没数据的话则直接显示问号头像
                $(peopleLi).find('img').attr('src','images/question.png');
                $(peopleLi).find('span').text('');
            }
        }





    })
    /* 点击开始按钮结束 */













    /* 双击标题全屏显示结束 */
    //控制全屏
    function enterfullscreen() { //进入全屏
        var docElm = document.documentElement;
        //W3C
        if (docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }
        //FireFox
        else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }
        //Chrome等
        else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
        //IE11
        else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
    }
    function exitfullscreen() { //退出全屏
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    }
    var a = 0;
    $('.lottery-title').on('click', function () {
        a++;
        a % 2 == 1 ? enterfullscreen() : exitfullscreen();
    })
    /* 双击标题全屏显示结束 */







